<?php

class AccountController extends Controller {

    public function actionIndex() {

        $model = new LoginForm;
        $model->via = 'student';
        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect('profile_update');
        }
        // display the login form
        $this->render('admin_login', array('model' => $model));
    }

    public function actionKonfirmasi() {
        $model = new KonfirmasiForm;

        // uncomment the following code to enable ajax-based validation

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'konfirmasi-form-konfirmasi-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }


        if (isset($_POST['KonfirmasiForm'])) {
            $model->attributes = $_POST['KonfirmasiForm'];
            if ($model->validate()) {
                // form inputs are valid, do something here
                $model->proses();
                $model->unsetAttributes();
            }
        }
        $this->render('konfirmasi_', array('model' => $model));
    }

    public function actionProfile_update() {
        $this->hashStudent();
        $model = $this->loadModel();

        // uncomment the following code to enable ajax-based validation

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'pelajar-profile_update-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }


        if (isset($_POST['Pelajar'])) {
            $model->attributes = $_POST['Pelajar'];
            if ($model->validate()) {
                // form inputs are valid, do something here
                $model->save();
                Yii::app()->user->setFlash('success', 'Profile berhasil dirubah');
            }
        }
        $model->tanggal_lahir = date('d-m-Y', strtotime($model->tanggal_lahir));
        $this->render('room_pelajar', array('model' => $model));
    }

    public function actionUpdate_image() {
        $this->hashStudent();
        $model = $this->loadModel();

        if (isset($_POST['Pelajar'])) {
            $model->attributes = $_POST['Pelajar'];
            $isImage = CUploadedFile::getInstance($model, 'image');
            if ($isImage !== null) {
                $dir = 'dokumen/photopelajar/' . str_replace('@', '-', Yii::app()->session['student']->email) . '.' . $isImage->extensionName;
                $isImage->saveAs($dir);
                $model->updateByPk(Yii::app()->session['student']->id_pelajar, array(
                    'image' => $dir
                ));
                $this->refresh();
            }
//            if ($model->validate()) {
            // form inputs are valid, do something here
//                $model->save();
//            }
        }
        $this->render('update_image', array('model' => $model));
    }

    public function actionUpdate_password() {
        $this->hashStudent();
        $model = new ChpassStudentForm;

        // uncomment the following code to enable ajax-based validation

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'chpass-student-form-chpass-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }


        if (isset($_POST['ChpassStudentForm'])) {
            $model->attributes = $_POST['ChpassStudentForm'];
            if ($model->validate()) {
                // form inputs are valid, do something here
//                return;
                $model->chpassword();
                Yii::app()->user->setFlash('success', 'password berhasil dirubah');
                $model->unsetAttributes();
            }
        }
        $this->render('update_password', array('model' => $model));
    }

    public function actionRuang_pelajar() {
        $this->render('room_view_pelajar');
    }

    public function loadModel() {
        $model = Pelajar::model()->findByPk(Yii::app()->session['student']->id_pelajar);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function hashStudent() {
        if (Yii::app()->session['student'] == NULL) {
            $this->redirect(Yii::app()->request->baseUrl);
        }
    }

}